package com.gurun.codes;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.support.v7.app.AppCompatDelegate;

import com.gurun.base.Logger;
import com.gurun.base.utils.SpUtils;
import com.gurun.base.utils.ToastManager;
import com.gurun.camera.account.activity.AccountUtil;
import com.gurun.camera.utils.CameraManager;
import com.gurun.codes.business.login.LoginActivity;
import com.gurun.codes.business.setting.SettingActivity;
import com.gurun.model.base.RetrofitManager;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class App extends MultiDexApplication {
    private static final String SP_KEY_TOKEN = "key_token";
    private static final String SP_KEY_USERID = "key_userid";
    private static final String SP_KEY_USER_NAME = "key_user_name";
    private static final String SP_KEY_USER_PHONE_NUM = "key_user_phone_num";

    private List<Activity> mActivitys = new ArrayList<Activity>();

    /*
     * 在5.0以下的设备上使用SVG
     */
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    private static App instance;

    public static App getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (instance == null) {
            instance = this;
        }
        RetrofitManager.init(this);
        Logger.init();

        //Jpush
        JPushInterface.setDebugMode(true);//TODO 发布版本需要改成false
        JPushInterface.init(this);
        AccountUtil.getInstance(this).setAppContext(this);
        //摄像头
        CameraManager.initCameraSdk(this);
    }

    public void addActivity(Activity activity) {
        mActivitys.add(activity);
    }

    public void removeAllActivity() {
        for (int i = 0; i < mActivitys.size(); i++) {
            mActivitys.get(i).finish();
        }
    }

    public void clearData() {
        SpUtils.getInstance(this).remove(SP_KEY_TOKEN);
        SpUtils.getInstance(this).remove(SP_KEY_USERID);
        SpUtils.getInstance(this).remove(SP_KEY_USER_PHONE_NUM);
    }

    /**
     * 注销第三方登录
     */
    private void removeThirdAcount() {

    }

    public void logOut() {
        removeAllActivity();
        clearData();
        removeThirdAcount();
    }

    /**
     * 设置登录token
     *
     * @param name
     */
    public void setUserName(String name) {
        SpUtils.getInstance(this).put(SP_KEY_USER_NAME, name);
    }

    public String getUserName() {
        return SpUtils.getInstance(this).getString(SP_KEY_USER_NAME, "");
    }

    /**
     * 设置登录手机号
     *
     * @param phoneNum
     */
    public void setUserPhoneNum(String phoneNum) {
        SpUtils.getInstance(this).put(SP_KEY_USER_PHONE_NUM, phoneNum);
    }

    public String getUserPhoneNum() {
        return SpUtils.getInstance(this).getString(SP_KEY_USER_PHONE_NUM, "");
    }

    /**
     * 设置登录token
     *
     * @param token
     */
    public void setLoginToken(String token) {
        SpUtils.getInstance(this).put(SP_KEY_TOKEN, token);
    }

    public String getLoginToken() {
        return SpUtils.getInstance(this).getString(SP_KEY_TOKEN, "");
    }

    /**
     * 设置登录userId
     *
     * @param userId
     */
    public void setLoginUserId(int userId) {
        SpUtils.getInstance(this).put(SP_KEY_USERID, userId);
    }

    public int getLoginUserId() {
        return SpUtils.getInstance(this).getInt(SP_KEY_USERID, -1);
    }

    /**
     * 判断用户是否已经登录
     *
     * @return
     */
    public boolean isLogin(Context context) {
        int loginUserId = getLoginUserId();
        if (loginUserId > 0) {
            return true;
        }
        return false;
    }

    /**
     * 检测用户是否已经登录，如果没登录提示并跳转到登录页面
     *
     * @param context
     * @return
     */
    public boolean checkLoginAndStartLoginActivity(Context context) {
        if (isLogin(context)) {
            return true;
        }
        ToastManager.show(context, R.string.login_not_login_tips);
        LoginActivity.startActivity(context);
        return false;
    }
}